using System;
using System.Web;

namespace SmartHRMS.NhibernateRepository
{
    public class NHibernateHttpModule : IHttpModule
    {
        public void Dispose()
        {
        }
		
        public void Init(System.Web.HttpApplication context)
        {
            context.EndRequest += new System.EventHandler(Application_EndRequest);
            context.BeginRequest += new System.EventHandler(Application_BeginRequest);
        }
		
        public void Application_BeginRequest(object sender, EventArgs e)
        {
            NHibernate.Context.CurrentSessionContext.Bind(SessionFactory.GetNewSession());
        }
		
        public void Application_EndRequest(object sender, EventArgs e)
        {
            NHibernate.ISession CurrentSession;
            CurrentSession = NHibernate.Context.CurrentSessionContext.Unbind(SessionFactory.GetSessionFactory());
			
            CurrentSession.Close();
            CurrentSession.Dispose();
        }
    }
}
